package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TbInspectorLog;
import com.ruoyi.system.domain.TbSearchlightQueue;
import com.ruoyi.system.domain.TbSearchlightRecord;
import com.ruoyi.system.domain.dto.SearchlightQueueDto;
import com.ruoyi.system.domain.vo.SearchlightQueueVo;
import com.ruoyi.system.mapper.TbInspectorLogMapper;
import com.ruoyi.system.mapper.TbSearchlightQueueMapper;
import com.ruoyi.system.mapper.TbSearchlightRecordMapper;
import com.ruoyi.system.service.ITbSearchlightService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 探照室Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@Service
public class TbSearchlightServiceImpl implements ITbSearchlightService
{
    @Autowired
    private TbSearchlightQueueMapper searchlightQueueMapper;

    @Autowired
    private TbSearchlightRecordMapper searchlightRecordMapper;

    @Autowired
    private TbInspectorLogMapper inspectorLogMapper;

    @Override
    public SearchlightQueueVo selectTbSearchlightQueueById(Long id) {
        SearchlightQueueVo vo = new SearchlightQueueVo();
        TbSearchlightQueue searchlightQueue = searchlightQueueMapper.selectTbSearchlightQueueById(id);
        BeanUtils.copyProperties(searchlightQueue, vo);
        vo.setSearchlightRecordList(searchlightRecordMapper.selectTbSearchlightRecordList(id));
        vo.setInspectorLogList(inspectorLogMapper.selectTbInspectorLogList(id));
        return vo;
    }

    @Override
    public List<TbSearchlightQueue> selectTbSearchlightQueueList(SearchlightQueueDto searchlightQueue) {
        return searchlightQueueMapper.selectTbSearchlightQueueList(searchlightQueue);
    }

    @Override
    public int insertTbSearchlightQueue(TbSearchlightQueue searchlightQueue) {
        searchlightQueue.setCreateBy(SecurityUtils.getUsername());
        searchlightQueue.setCreateTime(DateUtils.getNowDate());
        return searchlightQueueMapper.insertTbSearchlightQueue(searchlightQueue);
    }

    @Override
    public int updateTbSearchlightQueue(TbSearchlightQueue searchlightQueue) {
        searchlightQueue.setUpdateBy(SecurityUtils.getUsername());
        searchlightQueue.setUpdateTime(DateUtils.getNowDate());
        return searchlightQueueMapper.updateTbSearchlightQueue(searchlightQueue);
    }

    @Override
    public int deleteTbSearchlightQueueByIds(Long[] ids) {
        return searchlightQueueMapper.deleteTbSearchlightQueueByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTbSearchlightQueueByInspector(TbSearchlightQueue searchlightQueue) {
        // 获取修改前数据
        TbSearchlightQueue before = searchlightQueueMapper.selectTbSearchlightQueueById(searchlightQueue.getId());
        StringBuilder record = new StringBuilder();
        if (before.getPreInTime() != null && !before.getPreInTime().equals(searchlightQueue.getPreInTime())) {
            record.append("预计进入时间由").append(before.getPreInTime()).append("变更为").append(searchlightQueue.getPreInTime()).append(";");
        }
        if (before.getPreOutTime() != null && !before.getPreOutTime().equals(searchlightQueue.getPreOutTime())) {
            record.append("预计离开时间由").append(before.getPreOutTime()).append("变更为").append(searchlightQueue.getPreOutTime()).append(";");
        }
        if (before.getPreDate() != null && !before.getPreDate().equals(searchlightQueue.getPreDate())) {
            record.append("预计日期由").append(before.getPreDate()).append("变更为").append(searchlightQueue.getPreDate()).append(";");
        }
        // 保存质检员修改记录
        if (record.length() > 0) {
            TbSearchlightRecord searchlightRecord = new TbSearchlightRecord();
            searchlightRecord.setQueueId(searchlightQueue.getId());
            searchlightRecord.setRecord(record.toString());
            searchlightRecord.setCreateBy(SecurityUtils.getUsername());
            searchlightRecord.setCreateTime(DateUtils.getNowDate());
            searchlightRecordMapper.insertTbSearchlightRecord(searchlightRecord);
        }
        searchlightQueue.setUpdateBy(SecurityUtils.getUsername());
        searchlightQueue.setUpdateTime(DateUtils.getNowDate());
        return searchlightQueueMapper.updateTbSearchlightQueue(searchlightQueue);
    }

    @Override
    public int inspectorLog(TbInspectorLog inspectorLog) {
        if (inspectorLog.getId() != null && inspectorLog.getId() > 0) {
            inspectorLog.setUpdateBy(SecurityUtils.getUsername());
            inspectorLog.setUpdateTime(DateUtils.getNowDate());
            inspectorLogMapper.updateTbInspectorLog(inspectorLog);
        } else {
            inspectorLog.setCreateBy(SecurityUtils.getUsername());
            inspectorLog.setCreateTime(DateUtils.getNowDate());
            inspectorLogMapper.insertTbInspectorLog(inspectorLog);
        }
        return 1;
    }

    @Override
    public int searchlightInspect(TbSearchlightQueue searchlightQueue) {
        searchlightQueue.setUpdateBy(SecurityUtils.getUsername());
        searchlightQueue.setUpdateTime(DateUtils.getNowDate());
        DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("HH:mm");
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yy-MM-dd");
        if ("2".equals(searchlightQueue.getStatus())) {
            searchlightQueue.setActInTime(dtf1.format(LocalTime.now()));
            searchlightQueue.setActDate(dtf2.format(LocalDate.now()));
            searchlightQueue.setActInUser(SecurityUtils.getUsername());
        } else if ("3".equals(searchlightQueue.getStatus())) {
            searchlightQueue.setActOutTime(dtf1.format(LocalTime.now()));
            searchlightQueue.setActOutUser(SecurityUtils.getUsername());
        }
        return searchlightQueueMapper.searchlightInspect(searchlightQueue);
    }

    @Override
    public int issueTbSearchlightQueue(List<TbSearchlightQueue> list) {
        return searchlightQueueMapper.issueTbSearchlightQueue(list);
    }
}
